﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Resources;

namespace University.Workshop.WinApp
{
    public partial class StoreProductForm : TemplateForm
    {
        private RoomDataAccess room = new RoomDataAccess();
        private ResourceManager resours;
        public double costTotal;
        public string description;  
        string isValidRoom = null;            
        public StoreProductForm()
        {
            //labelData.Text = DateTime.Today.ToShortDateString();
            InitializeComponent();
            resours = new ResourceManager(typeof(RegisterAdministrator));
            dataGridViewStore.Rows.Clear();
            costTotal = 0;
            room.GetRoomInCheckIn(this.textClient);
            this.comboGroup.Text = "Lacteos";
            lCost.Text = "0";
        }

        private void bExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void comboGroup_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            DataSet product = new DataSet();
            string searchProducts = comboGroup.Text;
            ProductRegister goSearchProduct = new ProductRegister();
            dataGridViewStore.Rows.Clear();
            DataTable table = goSearchProduct.SearchProductByGroup(searchProducts);
            int countRows = table.Rows.Count;
            for (int i = 0; i < countRows; i++)
            {
                int test = dataGridViewStore.Rows.Add();
                dataGridViewStore.Rows[i].Cells[0].Value = table.Rows[i][0];
                dataGridViewStore.Rows[i].Cells[1].Value = table.Rows[i][1];
            }
        }
        private int Validator()
        {
            errorProviderStore.Clear();
            int count = 0;
            bool isValid = false;
            isValid = RegistrationProduct.IsValidCount(textQuantity.Text.Trim());
            if (isValid == false)
            {
                errorProviderStore.SetError(textQuantity,"Error");
                count++;
            }
            return count;
        }
        private int ValidatorRoom()
        {
            errorProviderStore.Clear();
            int count = 0;
            //string isValid = null;
            isValidRoom = RegistrationProduct.IsValidRoom(textClient.Text.Trim());
            if (isValidRoom == null)
            {
                errorProviderStore.SetError(textClient, "Error");
                count++;
            }
            return count;
        }
        private void bOk_Click(object sender, EventArgs e)
        {
            int isValid = 0;
            isValid = Validator();
            int count = 0;
            //lCost.Text = "0";
            if (isValid > 0)
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Debe escribir la contidad del producto", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);

            }
            else
            {
                ListViewItem item = new ListViewItem();

                DataGridViewCell selectProductName = dataGridViewStore.CurrentRow.Cells[0];
                string productName = selectProductName.Value.ToString();
                DataGridViewCell selectProductCost = dataGridViewStore.CurrentRow.Cells[1];
                double productCost = Convert.ToDouble(selectProductCost.Value.ToString());
                count = Convert.ToInt32(textQuantity.Text);
                description += productName + ",";
                description += productCost + ",";
                description += count + "-";
                costTotal = costTotal + (count * productCost);
                lCost.Text = Convert.ToString(costTotal);
                textQuantity.Text = "";
                item.SubItems.Add(productName);
                item.SubItems.Add(productCost.ToString());
                item.SubItems.Add(count.ToString());
                listViewStore.Items.Add(item);
            }
        }

        private void bAccept_Click(object sender, EventArgs e)
        {
            int isValid = 0;
            isValid = ValidatorRoom();
            //int count = 0;
            if (isValid > 0)
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "El cuarto no esta reservado o no exite", caption, buttons, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                double isSale = Convert.ToDouble(lCost.Text);
                if (isSale > 0)
                {
                    try
                    {
                        RegistrationProduct.RegisterSale(isValidRoom, textClient.Text.Trim(), lCost.Text.Trim(), description);
                        string caption = this.resours.GetString("messageBox.Caption");
                        MessageBoxButtons buttons = MessageBoxButtons.OK;
                        MessageBox.Show(this, "la venta se lo cargo a su cuenta", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                        //MessageBox.Show(description);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, "no se compro ningun producto", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }
            }
        }
    }
}
